package main

import (
	"net/http"

	"github.com/gin-gonic/gin"
)

/**
1、 可以返回那些数据类型
*/

func main() {

	engine := gin.Default()

	// 静态文件加载目录  --  访问路径 -- 资源路径
	engine.Static("004-return-data/static/img", "./004-return-data/static/img")
	// 设置 html 的加载目录
	engine.LoadHTMLGlob("./004-return-data/html/*")
	// 接口返回string类型
	engine.GET("/getStr", func(c *gin.Context) {
		path := "请求路径 = " + c.FullPath()
		c.Writer.WriteString(path)
	})
	// 以字节形式返回string
	engine.GET("/getByte", func(c *gin.Context) {
		path := "请求路径 = " + c.FullPath()
		c.Writer.Write([]byte(path))
	})
	// 返回json 内容
	engine.GET("/getJson", func(c *gin.Context) {
		path := "请求路径 = " + c.FullPath()
		c.JSON(http.StatusOK, map[string]interface{}{
			"path": path,
		})
	})
	// 返回结构体类型
	engine.GET("/getStruct", func(c *gin.Context) {
		path := "请求路径 = " + c.FullPath()
		c.JSON(http.StatusOK, &ResultMes{
			200, "我是傻逼", path,
		})
	})
	// 返回html 页面
	engine.GET("/getHtml", func(c *gin.Context) {
		path := "请求路径 = " + c.FullPath()
		c.HTML(http.StatusOK, "index.html", gin.H{
			"path":   path,
			"title":  "Gin",
			"imgUrl": "./004-return-data/static/img/image_01.jpg",
		})
	})
	engine.Run(":9000")

}

type ResultMes struct {
	Code    int    `json:"code"`
	Message string `json:"message"`
	Path    string `json:"path"`
}
